<!DOCTYPE html>
<html >
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="ruoleng" />



<meta name="description" content="Boost asio库只使用头文件的方式前言之前用过几个Boost的库，比如智能指针shared_ptr，bind，function，expressive都很好，加上工作中需要跟串口、网口通讯打交道，而之前用的都是自己的简单封装或者直接写了调用。所以就想用用asio。
开始用的那几个库都是根据《Boost程序库完全开发指南》学着用的，这次用的也是。
查找方法根据旧版(2010年9月第一版 ISBN">
<meta property="og:type" content="article">
<meta property="og:title" content="Boost 的 asio库只使用头文件模式">
<meta property="og:url" content="http://ruoleng.github.io/2017/04/30/cpp-boost-asio-header-only/index.html">
<meta property="og:site_name" content="ruoleng'blog">
<meta property="og:description" content="Boost asio库只使用头文件的方式前言之前用过几个Boost的库，比如智能指针shared_ptr，bind，function，expressive都很好，加上工作中需要跟串口、网口通讯打交道，而之前用的都是自己的简单封装或者直接写了调用。所以就想用用asio。
开始用的那几个库都是根据《Boost程序库完全开发指南》学着用的，这次用的也是。
查找方法根据旧版(2010年9月第一版 ISBN">
<meta property="og:updated_time" content="2017-04-30T16:05:06.909Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Boost 的 asio库只使用头文件模式">
<meta name="twitter:description" content="Boost asio库只使用头文件的方式前言之前用过几个Boost的库，比如智能指针shared_ptr，bind，function，expressive都很好，加上工作中需要跟串口、网口通讯打交道，而之前用的都是自己的简单封装或者直接写了调用。所以就想用用asio。
开始用的那几个库都是根据《Boost程序库完全开发指南》学着用的，这次用的也是。
查找方法根据旧版(2010年9月第一版 ISBN">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternative" href="/atom.xml" title="ruoleng&#39;blog" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>Boost 的 asio库只使用头文件模式 | ruoleng&#39;blog</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        open_in_new: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: undefined
    }
</script>


    <script> yiliaConfig.jquery_ui = [false]; </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">ruoleng</a></h1>
        </hgroup>

        
        <p class="header-subtitle">不积硅步无以至千里 | 生命不息 折腾不止</p>
        

        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/atom.xml" title="RSS"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Boost/">Boost</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/asio/">asio</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/header-only/">header only</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vs2017/">vs2017</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">我竟无言以对</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">ruoleng</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">ruoleng</a></h1>
            </hgroup>
            
            <p class="header-subtitle">不积硅步无以至千里 | 生命不息 折腾不止</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/atom.xml" title="RSS"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-cpp-boost-asio-header-only" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2017/04/30/cpp-boost-asio-header-only/" class="article-date">
      <time datetime="2017-04-29T16:35:05.000Z" itemprop="datePublished">2017-04-30</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Boost 的 asio库只使用头文件模式
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        

        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Boost/">Boost</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/C/">C++</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/asio/">asio</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/header-only/">header only</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vs2017/">vs2017</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <h1 id="Boost-asio库只使用头文件的方式"><a href="#Boost-asio库只使用头文件的方式" class="headerlink" title="Boost asio库只使用头文件的方式"></a>Boost asio库只使用头文件的方式</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>之前用过几个Boost的库，比如智能指针shared_ptr，bind，function，expressive都很好，<br>加上工作中需要跟串口、网口通讯打交道，而之前用的都是自己的简单封装或者直接写了调<br>用。所以就想用用asio。</p>
<p>开始用的那几个库都是根据《Boost程序库完全开发指南》学着用的，这次用的也是。</p>
<h2 id="查找方法"><a href="#查找方法" class="headerlink" title="查找方法"></a>查找方法</h2><p>根据旧版(2010年9月第一版 ISBN 978-121-11577-6)书中 12.2章（493页）所描述，<br>加上以下宏定义<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#define  BOOST_DATE_TIME_SOURCE</span><br><span class="line">#define  BOOST_SYSTEM_NO_LIB</span><br><span class="line">#define  BOOST_REGEX_NO_LIB</span><br></pre></td></tr></table></figure></p>
<p>就可以不用编译boost的库使用。但是因为这次不在原来的电脑上写程序，在网上搜了此书<br>发现出了新版，然而新版(2015年3月第一版 ISBN 978-121-25313-3) 12.3章(537页)中并没<br>有涉及到不需要<br>编译Boost库而使用asio库的方法，只提到使用asio库的方法：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#define  BOOST_ASIO_DISABLE_STD_CHRONO</span><br><span class="line">#include &lt;boost/asio.hpp</span><br><span class="line">using namespace boost::asio;</span><br></pre></td></tr></table></figure></p>
<p>而Boost的帮助文件中则没有任何只用头文件的说明，只说到不自动链接Boost.Date_Time和<br>Boost.Regex的方法（在链接的命令行中添加 -DBOOST_DATE_TIME_NO_LIB 和<br>-DBOOST_REGEX_NO_LIB）；通过Google搜索也只搜到StackOverFlow上的提问，因为无论怎么<br>设置都无法避开Boost的System的lib链接。</p>
<p>通过查看书中的System说明，同时百度也随便搜了其他的方法，终于找到了解决的方法<br>增加多<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#define  BOOST_ERROR_CODE_HEADER_ONLY</span><br></pre></td></tr></table></figure></p>
<h2 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h2><p>这个宏的定义就可以以头文件的方式使用Boost.System库。<br>所以要以头文件方式使用Boost.asio库要定义的头文件为<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#define  BOOST_ERROR_CODE_HEADER_ONLY</span><br><span class="line">#define  BOOST_DATE_TIME_SOURCE</span><br><span class="line">#define  BOOST_SYSTEM_NO_LIB</span><br><span class="line">#define  BOOST_REGEX_NO_LIB</span><br><span class="line">#include &lt;boost/asio.hpp</span><br><span class="line">//using namespace boost::asio;  看自己需要是否要加入命名空间的使用</span><br></pre></td></tr></table></figure></p>
<p>增加后在VS2017下编译通过。</p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2017/04/30/cpp-boost-asio-header-only/">Boost 的 asio库只使用头文件模式</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">ruoleng</a></p>
        <p><span>发布时间:</span>2017-04-30, 00:35:05</p>
        <p><span>最后更新:</span>2017-05-01, 00:05:06</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2017/04/30/cpp-boost-asio-header-only/" title="Boost 的 asio库只使用头文件模式">http://ruoleng.github.io/2017/04/30/cpp-boost-asio-header-only/</a>
            <span class="copy-path" data-clipboard-text="原文: http://ruoleng.github.io/2017/04/30/cpp-boost-asio-header-only/　　作者: ruoleng" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2017/04/30/hello-world/">
                    你好 博客
                </a>
            </div>
        
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Boost-asio库只使用头文件的方式"><span class="toc-number">1.</span> <span class="toc-text">Boost asio库只使用头文件的方式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#前言"><span class="toc-number">1.1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查找方法"><span class="toc-number">1.2.</span> <span class="toc-text">查找方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#解决方法"><span class="toc-number">1.3.</span> <span class="toc-text">解决方法</span></a></li></ol></li></ol>
</div>
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
</style>

<input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

<script>
    yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
</script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"Boost 的 asio库只使用头文件模式　| ruoleng'blog　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
        <section class="youyan" id="comments">
    <script>
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = 'http://v2.uyan.cc/code/uyan.js?uid=2103384';
            (d.head || d.body).appendChild(s);
        }
    </script>
    
    <script> loadComment(); </script>

    <div id="uyan_frame"></div>
</section>
    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2017/04/30/hello-world/" title="上一篇: 你好 博客">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/" title="回到主页"><i class="fa fa-home"></i></a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2017/04/30/hello-world/">你好 博客</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/04/30/cpp-boost-asio-header-only/">Boost 的 asio库只使用头文件模式</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2017 ruoleng
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.0">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" >本站到访数: 
                            <span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>, </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit">本页阅读量: 
                            <span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>




<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>